/** 
  其他境外交流项目管理-导师/学院负责人/学院领导审核出国
*/
<template>
  <div class="project-application-container">
    <div style="height: 30px;">
      <el-breadcrumb style="float: left;" separator="/">
        <el-breadcrumb-item :to="{ path: '/exchangeProjectAudit' }">
          <span @click="reback()">{{ $t('projectManage.otherOverseasExchangeAudit') }}</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>{{ $t('externamExchangeBase.audit') }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card shadow="never" v-loading="loading" body-style="padding: 24px;">
      <div slot="header" class="title">
        <span>{{ $t('projectManage.otherOverseasExchangeAudit') }}</span>
         - 
        {{ $t('externamExchangeBase.audit') }}
        <el-button style="float:right; margin-top: -5px;" @click="reback()">{{ $t('common.btn.back') }}</el-button>
      </div>
      <go-abroad-common-info :applyId="applyId">
        <div class="audit-container">
          <template>
            <!-- 审批明细开始 -->
            <div class="privacy-title title other-title" style="margin-top: 40px;">
              {{ $t('exchangeProjectAudit.approvalDetail') }}
            </div>
            <el-form :model="auditInfo" ref="auditInfo" validate-on-rule-change>
              <el-row :gutter="24">
                <el-col :span="24">
                  <!-- 审核意见 -->
                  <el-form-item
                    :label="$t('exchangeProjectAudit.auditOpinion') + '：'" 
                    prop="content"
                    :rules="[{ validator: checkSpecial, trigger: 'blur'}]">
                      <el-input v-model="auditInfo.content" :maxlength="500" type="textarea" :rows="7" clearable :placeholder="$t('common.pla.pleaseInput')"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24" v-if="(applyActExpert === 0 && subsidizeFillIn === 1 && this.projectSubCategory === 7) || showFunding">
                <el-col :span="8">
                  <!-- 资助期限 -->
                  <el-form-item
                    :label="$t('projectApplication.projectFundingPeriod') + '：'" 
                    prop="projectFundingPeriod"
                    :rules="projectFundingPeriodArr">
                      <el-input v-model="auditInfo.projectFundingPeriod" :maxlength="9" clearable :placeholder="$t('common.pla.pleaseInput')"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <!-- 资助标准 -->
                  <el-form-item
                    :label="$t('exchangeProjectAudit.subsidizeStandard') + '：'" 
                    prop="subsidizeStandard"
                    :rules="subsidizeStandardArr">
                      <el-input v-model="auditInfo.subsidizeStandard" :maxlength="32" clearable :placeholder="$t('common.pla.pleaseInput')"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <!-- 资助金额 -->
                  <el-form-item
                    :label="$t('exchangeProjectAudit.subsidizeAmount') + '：'" 
                    prop="subsidizeAmount"
                    :rules="subsidizeAmountArr">
                      <el-input v-model="auditInfo.subsidizeAmount" :maxlength="32" clearable :placeholder="$t('common.pla.pleaseInput')"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div class="operate-footer">
              <template v-if="permitToExpertsAudit">
                <el-button @click="cancel()">{{ $t('common.btn.cancel') }}</el-button>
                <el-button type="primary" @click="submit()">{{ $t('common.btn.submit') }}</el-button>
              </template>
              <template v-if="permitToNotExpertsAudit">
                <el-button type="danger" @click="reject()">{{ $t('externamExchangeBase.reject') }}</el-button>
                <el-button type="primary" @click="notPass()">{{ $t('externamExchangeBase.notPass') }}</el-button>
                <el-button type="primary" @click="judgePass()">{{ $t('externamExchangeBase.pass') }}</el-button>
              </template>
            </div>
            <!-- 审批明细结束 -->
          </template>
          <!-- 审批进度开始 -->
          <template slot="footer"></template>
          <!-- 审批进度结束 -->
        </div>
      </go-abroad-common-info>
      <!-- 是否要指定专家弹框开始 -->
      <el-dialog
        :title="$t('exchangeProjectAudit.specifyExperts')"
        width="20%"
        :visible.sync="whetherToExpertsDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false">
        <div class="dialog-footer" style="text-align: center;">
          <template>
            <el-button type="primary" @click="specify()">{{ $t('exchangeProjectAudit.specify') }}</el-button>
            <el-button type="primary" @click="notSpecify()">{{ $t('exchangeProjectAudit.notSepcify') }}</el-button>
          </template>
        </div>
      </el-dialog>
      <!-- 是否要指定专家弹框结束 -->
      <!-- 指定专家弹框开始 -->
      <template v-if="specifyExpertsDialog">
        <el-dialog
          :title="$t('exchangeProjectAudit.specifyPeople')"
          width="50%"
          :visible.sync="specifyExpertsDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false">
            <experts-info ref="expertsInfomation" @change-current-experts="changeCurrentExperts"></experts-info>
            <span slot="footer">
              <template>
                <el-button @click="cancelSpecify()">{{ $t('common.btn.cancel') }}</el-button>
                <el-button type="primary" @click="specifyExperts()">{{ $t('common.btn.confirm') }}</el-button>
              </template>
            </span>
        </el-dialog>
      </template>
      <!-- 指定专家弹框结束 -->
    </el-card>
  </div>
</template>
<script>
import goAbroadCommonInfo from 'common/src/views/externalExchange/common/goAbroadCommonInfo';
import externalExchangeApi from 'common/src/api/externalExchange/externalExchange';
import expertsInfo from 'common/src/views/externalExchange/common/expertsInfo';
export default {
  components: {
    'go-abroad-common-info': goAbroadCommonInfo,
    'experts-info': expertsInfo
  },
  data() {
    return {
      auths: [],
      auditInfo: {
        content: '',
        projectFundingPeriod: '',
        subsidizeStandard: '',
        subsidizeAmount: ''
      },
      projectFundingPeriodArr: [
        { validator: this.checkSpecial, trigger: 'blur' },
        { validator: this.checkPositiveInteger, trigger: 'blur'}
      ],
      subsidizeStandardArr: [
        { validator: this.checkSpecial, trigger: 'blur' }
      ],
      subsidizeAmountArr: [
        { validator: this.checkSpecial, trigger: 'blur' }
      ],
      showFunding: false,
      whetherToExpertsDialog: false,
      specifyExpertsDialog: false,
      currentExpertCode: '',
      permitToExpertsAudit: false,
      permitToNotExpertsAudit: false
    };
  },
  props: {
    applyId: {
      type: String,
      default: ''
    },
    applyActExpert: {
      type: Number,
      default: null
    },
    subsidizeFillIn: {
      type: Number,
      default: null
    },
    projectSubCategory: {
      type: Number,
      default: null
    }
  },
  computed: {
  },
  filters: {
  },
  created() {
    // 获取当前用户auths权限
    if (localStorage.getItem("sessiondata")) {
      if (JSON.parse(localStorage.getItem("sessiondata")).auths) {
        this.auths = JSON.parse(localStorage.getItem("sessiondata")).auths;
      }
    }
  },
  async mounted() {
  
  },
  methods: {
    // 校验特殊字符$
    checkSpecial(rule, value, callback) {
      let regEn = /[`$]/im;
      if (regEn.test(value)) {
        callback(new Error(this.$t('graduateBase.specialText')))
      } else {
        callback();
      }
    },
    // 校验申请期限
    checkPositiveInteger(rule, value, callback) {
      let regExp = /^[1-9]\d*$/;
      if (value && !regExp.test(value)) {
        callback(new Error(this.$t('externamExchangeBase.validatePositiveInteger')))
      } else {
        callback();
      }
    },
    reback() {
      this.$emit('change-project-index')
    },
    // 驳回
    reject() {
      this.$refs['auditInfo'].clearValidate()
      this.$set(this.projectFundingPeriodArr, 2, {})
      this.$set(this.subsidizeStandardArr, 1, {})
      this.$set(this.subsidizeAmountArr, 1, {})
      if (this.auditInfo.content) {
        this.$refs['auditInfo'].validate((valid) => {
          if (valid) {
            externalExchangeApi.auditApplyProjectApp({
              "type": 2,
              "content": this.auditInfo.content,
              "applyIds": [this.applyId]
            }).then(res => {
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: this.$t('externamExchangeBase.auditSuccessfully')
                })
                this.reback()
              }
            })
          } else {
            setTimeout(() => {
              let isError = document.getElementsByClassName("is-error");
              if (isError[0] && isError[0] && isError[0].querySelector('input')) {
                isError[0].querySelector('input').focus();
              }
            }, 1);
          }
        })
      } else {
        this.$message.error(this.$t('exchangeProjectAudit.requireAuditOpinion'))
      }
    },
    // 不通过
    notPass() {
      this.$refs['auditInfo'].clearValidate()
      this.$set(this.projectFundingPeriodArr, 2, {})
      this.$set(this.subsidizeStandardArr, 1, {})
      this.$set(this.subsidizeAmountArr, 1, {})
      this.$refs['auditInfo'].validate((valid) => {
        if (valid) {
          externalExchangeApi.auditApplyProjectApp({
            "type": 0,
            "content": this.auditInfo.content,
            "applyIds": [this.applyId]
          }).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: this.$t('externamExchangeBase.auditSuccessfully')
              })
              this.reback()
            }
          })
        } else {
          setTimeout(() => {
            let isError = document.getElementsByClassName("is-error");
            if (isError[0] && isError[0] && isError[0].querySelector('input')) {
              isError[0].querySelector('input').focus();
            }
          }, 1);
        }
      })
    },
    // 通过
    pass() {
      this.$refs['auditInfo'].clearValidate()
      this.$set(this.projectFundingPeriodArr, 2, { required: true, message: this.$t('projectApplication.requireProjectFundingPeriod'), trigger: 'blur'})
      this.$set(this.subsidizeStandardArr, 1, { required: true, message: this.$t('exchangeProjectAudit.requireSubsidizeStandard'), trigger: 'blur'})
      this.$set(this.subsidizeAmountArr, 1, { required: true, message: this.$t('exchangeProjectAudit.requireSubsidizeAmount'), trigger: 'blur'})
      this.$refs['auditInfo'].validate((valid) => {
        if (valid) {
          let params = {
            "type": 1,
            "applyIds": [this.applyId]
          }
          if (this.auditInfo.content) {
            params['content'] = this.auditInfo.content
          }
          if (this.showFunding || (this.applyActExpert === 0 && this.subsidizeFillIn === 1 && this.projectSubCategory === 7)) {
            params['subsidizePeriod'] = Number(this.auditInfo.projectFundingPeriod)
            params['subsidizeStandard'] = this.auditInfo.subsidizeStandard
            params['subsidizeAmount'] = this.auditInfo.subsidizeAmount
          }
          externalExchangeApi.auditApplyProjectApp(params).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: this.$t('externamExchangeBase.auditSuccessfully')
              })
              this.reback()
            }
          })
        } else {
          setTimeout(() => {
            let isError = document.getElementsByClassName("is-error");
            if (isError[0] && isError[0] && isError[0].querySelector('input')) {
              isError[0].querySelector('input').focus();
            }
          }, 1);
        }
      })
    },
    // 判断通过
    judgePass() {
      if (this.showFunding) {
        this.pass()
      } else if (this.applyActExpert) {
        this.whetherToExpertsDialog = true
      } else {
        this.pass()
      }
    },
    // 指定专家
    specify() {
      this.whetherToExpertsDialog = false
      this.specifyExpertsDialog = true
    },
    // 不指定专家
    notSpecify() {
      externalExchangeApi.specifyExpertsAudit({
        "applyIdList": [this.applyId],
        "teacherCode": ""
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t('externamExchangeBase.operateSuccessfully'))
          this.whetherToExpertsDialog = false
          if (this.subsidizeFillIn === 1 && this.projectSubCategory === 7) {
            this.showFunding = true
          } else {
            this.pass()
          }
        }
      })
    },
    // 选择专家
    changeCurrentExperts(row) {
      if (row) {
        this.currentExpertCode = row.teacherCode
      }
    },
    // 取消指定专家
    cancelSpecify() {
      this.specifyExpertsDialog = false
    },
    // 确定指定专家
    specifyExperts() {
      externalExchangeApi.specifyExpertsAudit({
        "applyIdList": [this.applyId],
        "teacherCode": this.currentExpertCode
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t('externamExchangeBase.operateSuccessfully'))
          this.specifyExpertsDialog = false
          this.pass()
        }
      })
    },
    // 取消
    cancel() {
      this.reback()
    },
    // 提交
    submit() {
      if (!this.auditInfo.content) {
        this.$message.error(this.$t('exchangeProjectAudit.requireAuditOpinion'))
        return
      }
      this.pass()
    }
  },
  watch: {
    auths: {
      handler(newVal) {
        if (newVal && Array.isArray(newVal) && newVal.length > 0) {
          newVal.some(item => {
            // 专家评审
            if (item.authCode === 'internCommuni-experts-audit') {
              this.permitToExpertsAudit = true;
            }
            // 非专家评审
            if (item.authCode === 'internCommuni-audit-not-experts') {
              this.permitToNotExpertsAudit = true;
            }
          });
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>
  .project-application-container {
    .el-breadcrumb__item:last-child ::v-deep {
      .el-breadcrumb__inner {
        color: #606266;
        font-weight: bold;
        cursor: default
      }
    }
    .el-breadcrumb__item ::v-deep {
      .is-link {
        color: #aaa;
        font-weight: unset;
      }
      .is-link:hover {
        color: #409eff;
      }
    }
    .title {
      font-weight: bold;
      font-size: 18px;
    }
    .privacy-title {
      margin-bottom: 20px;
    }
    .other-title {
      margin-top: 20px;
    }
    .audit-container {
      padding-left: 15px;
    }
    .operate-footer {
      text-align: right;
    }
    .self-loading {
      font-size: 40px;
    }
  }
</style>